import re
from .url_utils import parse_url_get_info, get_redirect_url, extract_or_return_url, get_platform
from .platform_utils import extract_title_img_name_url, fetch_platform_data
from setting import PLATFORM_LIS


def fillter_text(content: str):
    # 匹配格式如：【京东】https://3.cn/2ihfHx-C 「滴露健康沐浴露薄荷950g」



    ################################################ 1. 尝试匹配商品链接  ################################################
    # link_pattern = r'((https?://)?[^\s]+)'
    
    # 需要匹配的文本示例：
    # https://3.cn/2j-gx91F  (完整URL)
    # 3.cn/2j-gx91F        (省略协议的URL)
    # 310.78-10fl=300.78   (不应该匹配的数字表达式)
    # 
    # 正确的正则表达式应该匹配前两个，不匹配第三个
    # 建议使用：r'([https?://]?[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(?:/[^\s]*)?)'
    
    
    link_pattern = r'((?:https?://)?[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(?:/[^\s]*)?)'

    urls = re.findall(link_pattern, content)

    temp_list1 = []
    temp_list2 = []
    
    for url in urls:
        if "http" not in url:
            url = "https://" + url

        productId, platform_name = parse_url_get_info(get_redirect_url(extract_or_return_url(url).strip()))
        
        # 先获取商品ID 再调用商品详情接口获取完整信息
        content = content.replace(url, "endend占位符1endend")
        if productId:
            temp_list1.insert(0,
                                {"type": "link", "text": url, "product_id": productId, "platform_name": platform_name, "link_text": url})
        else:
            temp_list1.insert(0,
                                {"type": "link2", "text": url, "platform_name": get_platform(url), "link_text": url})
            
    ################################################ 2. 尝试匹配货号  ################################################
    sku_pattern = r'(\d{7,20})'
    sku_matchs = re.findall(sku_pattern, content)

    for sku in sku_matchs:
        for platform in PLATFORM_LIS:
            product_info = fetch_platform_data(sku, platform)  # 一般都是京东
            if not product_info:
                continue
            
            content = content.replace(sku, "endend占位符2endend")
            temp_list2.insert(0,
                                {
                                    "type": "link",
                                    "text": extract_title_img_name_url(product_info,platform).get("productUrl",None),
                                    "product_id": sku,
                                    "platform_name": platform,
                                    "link_text": "网页链接"}
                                )
            break
    result = []
    
    content_list = content.split("endend")
    
    for i in range(len(content_list)):
        if content_list[i] == "":
            continue
        if content_list[i] == "占位符1":
            result.append(temp_list1.pop())
        elif content_list[i] == "占位符2":
            result.append(temp_list2.pop())
        else:
            result.append(
                {
                    "type": "text",
                    "text": content_list[i]
                }
            ) 
    return result 